let app = getApp()
Page({
  data: {
    couponItem: [],
    couponType: ''
  },
  // 添加优惠券
  addCoupon(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    if (that.data.couponType.type === 'couponUgain') {
      wx.request({
        url: app.globalData.data_url + '/GPD/addConsumerCoupon.do',
        data: {
          coupon_id: e.currentTarget.dataset.couponid,
          session_id: app.globalData.sessionId
        },
        success(res) {
          console.dir(res)
          if (res.data.code == '0') {
            wx.showToast({
              title: '领取成功！'
            })
            var couponItem = that.data.couponItem
            couponItem.splice(index, 1)
            that.setData({
              couponItem: couponItem
            })
          } else {
            wx.showToast({
              title: res.data.message,
              image: '/images/close.png'
            })
          }
        }
      })
    }

  },
  // 格式化时间
  //返回对象
  // {
  //   y:年,
  //   m:月,
  //   d:日,
  //   day:星期,
  //   h:小时,
  //   mi:分,
  //   s:秒,
  // }
  // 参数
  // timeStamp:从1970年至今的毫秒数
  formatDate(timeStamp) {
    let temDate = new Date(timeStamp)
    let y = temDate.getFullYear()
    let m = temDate.getMonth()
    let d = temDate.getDate()
    let day = temDate.getDay()
    let h = temDate.getHours()
    let mi = temDate.getMinutes()
    let s = temDate.getSeconds()
    return {
      y,
      m,
      d,
      day,
      h,
      mi,
      s
    }
  },
  onLoad: function (options) {
    let that = this
    console.log(options.type)
    that.setData({
      couponType: options
    })
    if (options.type == 'couponUgain') {
      wx.request({
        url: app.globalData.data_url + '/GPD/getIndexCoupon.do',
        data: {
          session_id: app.globalData.sessionId
        },
        success: function (res) {
          let temDate = res.data.coupons.map(val=>{
            let temStartDate=that.formatDate(val.startdate)
            let temEndDate=that.formatDate(val.enddate)
            val.showStartDate = `${temStartDate.m}.${temStartDate.d} ${temStartDate.h < 10 ? ('0' + temStartDate.h) : temStartDate.h}:${temStartDate.mi < 10 ? ('0' + temStartDate.mi) : temStartDate.mi} - ${temEndDate.m}.${temEndDate.d} ${temEndDate.h < 10 ? ('0' + temEndDate.h) : temEndDate.h}:${temEndDate.mi < 10 ? ('0' + temEndDate.mi) : temEndDate.mi}`
            return val
          })
          that.setData({
            couponItem: temDate
          })
          console.log(that.data.couponItem)
        }
      })
    } else if (options.type == 'couponMy') {
      wx.request({
        url: app.globalData.data_url + '/GPD/getConsumerCoupon.do',
        data: {
          session_id: app.globalData.sessionId
        },
        success(res) {
          // console.dir(new Date(res.data.coupons[0].startdate)
          let temDate = res.data.coupons.map(val => {
            let temStartDate = that.formatDate(val.startdate)
            let temEndDate = that.formatDate(val.enddate)
            val.showStartDate = `${temStartDate.m}.${temStartDate.d} ${temStartDate.h < 10 ? ('0' + temStartDate.h) : temStartDate.h}:${temStartDate.mi < 10 ? ('0' + temStartDate.mi) : temStartDate.mi} - ${temEndDate.m}.${temEndDate.d} ${temEndDate.h < 10 ? ('0' + temEndDate.h) : temEndDate.h}:${temEndDate.mi < 10 ? ('0' + temEndDate.mi) : temEndDate.mi}`
            return val
          })
          that.setData({
            couponItem: temDate
          })
        }
      })
    }
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  }
})		